import time

from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
from utils import UtilsAppDriver


class BasePage:
    def __init__(self):
        self.driver = UtilsAppDriver.get_app_driver()

    def get_element(self, type_and_value):
        """
        根据传入类型进行相关搜索
        :param driver: 驱动driver
        :param type_and_value: 元组对象, type: 获取类型, By.Xpath  value: 对应方式取值
        :return: 获取元素对象
        """
        wait = WebDriverWait(self.driver, 10)
        element = wait.until(lambda x: x.find_element(*type_and_value))
        return element


# 基础操作类
class BaseHandler:
    def __init__(self):
        self.driver = UtilsAppDriver.get_app_driver()

    def input_text(self, element, text):
        """
        传入元素和输入的问题
        :param element: 元素对象
        :param text: 要输入的内容
        """
        element.clear()
        element.send_keys(text)

    def is_element_exist(self, type_and_value):
        """
        判断页面元素是否存在
        :param driver: 驱动
        :param type_and_value: type_and_value: 元组对象, type: 获取类型, By.Xpath  value: 对应方式取值
        :return: 元素是否存在
        """
        try:
            wait = WebDriverWait(self.driver, 10)
            wait.until(lambda x: x.find_element(*type_and_value))
            return True
        except Exception as exc:
            print(exc)
            return False

    # 移动端APP相关操作
    # class UtilsAppDriver(object):

    def swipe_execute(self, direction, times=1, duration=1000):
        """
        执行滑动操作
        :param driver: appium驱动
        :param direction: 向上滑动:up, 向下:down, 向左:left, 向右:right
        :param times: 滑动次数
        :param duration: 滑动持续时间,默认2000ms
        """
        width = self.driver.get_window_size()['width']
        height = self.driver.get_window_size()['height']

        if direction == 'up':
            position = (width / 2, height * 0.9, width / 2, height * 0.1)
        elif direction == 'down':
            position = (width / 2, height * 0.1, width / 2, height * 0.9)
        elif direction == 'left':
            position = (width * 0.9, height / 2, width * 0.1, height / 2)
        elif direction == 'right':
            position = (width * 0.1, height / 2, width * 0.9, height / 2)
        else:
            position = (width / 2, height / 2, width / 2, height / 2)
            print('输入正确方向:向上:up, 向下:down, 向左:left, 向右:right')

        # 进入页面就滑动会报错
        time.sleep(0.5)

        for i in range(times):
            self.driver.swipe(*position, duration=duration)
            time.sleep(duration / 1000)

    def swipe_and_find_element(self, swipe_type_and_value, find_type_and_value, direction, duration=1000, timeout=30):
        """
        边滑动边寻找某元素,找到后返回该元素,找不到返回False
        :param driver: 手机驱动
        :param swipe_type_and_value: 要滑动的目标 元组对象, type: 获取类型, By.Xpath  value: 对应方式取值
        :param direction: 滑动的方向 向上滑动:up, 向下:down, 向左:left, 向右:right
        :param find_type_and_value: 想要查询的目标 元组对象, type: 获取类型, By.Xpath  value: 对应方式取值
        :param duration: 持续时间,默认1000ms
        :param timeout: 滑动寻找设置超时时间, 超过设置时间后返回false, 默认30s
        :return: 找到:返回找到的元素, 找不到返回False
        """

        # 元素本身属性方位大小
        try:
            wait = WebDriverWait(self.driver, 10)
            swipe_element = wait.until(lambda x: x.find_element(*swipe_type_and_value))

            ele_w = swipe_element.size['width']
            ele_h = swipe_element.size['height']
            ele_x = swipe_element.location['x']
            ele_y = swipe_element.location['y']

            if direction == 'up':
                position = (ele_x + ele_w / 2, ele_y + ele_h * 0.9, ele_x + ele_w / 2, ele_y + ele_h * 0.1)
            elif direction == 'down':
                position = (ele_x + ele_w / 2, ele_y + ele_h * 0.1, ele_x + ele_w / 2, ele_y + ele_h * 0.9)
            elif direction == 'left':
                position = (ele_x + ele_w * 0.9, ele_y + ele_h / 2, ele_x + ele_w * 0.1, ele_y + ele_h / 2)
            elif direction == 'right':
                position = (ele_x + ele_w * 0.1, ele_y + ele_h / 2, ele_x + ele_w * 0.9, ele_y + ele_h / 2)
            else:
                position = (ele_x + ele_w / 2, ele_y + ele_h / 2, ele_x + ele_w / 2, ele_y + ele_h / 2)
                print('输入正确方向:向上:up, 向下:down, 向左:left, 向右:right')

            start_time = time.time()
            end_time = 0
            while True:
                # 记录下查找前页面资源, 通过对比页面资源退出死循环
                if int(end_time - start_time) < timeout:
                    if int(end_time - start_time) > 0:
                        # 大于0时在打印
                        print(f'运行了{int(end_time - start_time)}s了')
                    page = self.driver.page_source
                    try:
                        fin_element = self.driver.find_element(*find_type_and_value)
                        return fin_element
                    except Exception as exc:
                        print('本屏幕没有找到该元素')
                        self.driver.swipe(*position, duration=duration)
                        time.sleep(duration / 1000)
                        if page == self.driver.page_source:
                            print('滑屏操作完成并且没有找到元素信息')
                            return False
                    finally:
                        end_time = time.time()
                else:
                    print(f'滑动寻找操作超过预设{timeout}s, 未找到该元素')
                    return False
        except Exception as exc:
            print('将要滑动的控件不存在, 修改获取控件方法后再试')
            return False

    # 获取toast消息
    def get_toast(driver, message, timeout=3):
        """
        根据部分信息,获取完整toast提示
        :param driver: 驱动
        :param message: 弹窗部分信息
        :param timeout: 获取toast的超时时间
        :return: 完整的toast消息
        """
        xpath = '//*[contains(@text, "{}")]'.format(message)
        try:
            wait = WebDriverWait(driver, timeout)
            element = wait.until(lambda x: x.find_element(By.XPATH, xpath))
            return element.text
        except Exception as exc:
            return '没有获取到 {} 相关的toast消息'.format(message)


# 基础逻辑类
class BaseProxy:

    def __init__(self):
        self.driver = UtilsAppDriver.get_app_driver()

    def is_element_exist(self, type_and_value):
        """
        判断页面元素是否存在
        :param type_and_value: type_and_value: 元组对象, type: 获取类型, By.Xpath  value: 对应方式取值
        :return: 元素是否存在
        """
        try:
            wait = WebDriverWait(self.driver, 5)
            wait.until(lambda x: x.find_element(*type_and_value))
            return True
        except Exception as exc:
            # print(exc)
            return False
